package com.data.structure.chapter1;

/**
 * Created by wangbing on 2018/3/15.
 */
// 一般数组的排序
public class SelectionSort01 {

    private  SelectionSort01(){}

    public static void sort(int[] arr){
        int n = arr.length;

        for (int i=0;i<n;i++ ){
            int minIndex = i;
            for(int j=i+1;j<n;j++){
                if(arr[j]<arr[minIndex]){
                    minIndex=j;
                }
//                交换元素位置
             swap(arr,i,minIndex);
            }
        }
    }

    private static void swap(int[] arr, int i, int minIndex) {
        int t = arr[i];
        arr[i]=arr[minIndex];
        arr[minIndex]=t;
    }

    public static void main(String[] args) {
        int[] arr = {16,34,5,6,52,87,9};
        sort(arr);
        for (int i =0;i<arr.length; i++){
            System.out.println(arr[i]);
        }
    }
}
